package com.tdh.service;

import com.tdh.dao.CkXzdwMapper;
import com.tdh.pojo.CkXzdw;
import com.tdh.pojo.TsDm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class CkXzdwService {

    @Autowired
    private CkXzdwMapper xzdwMapper;

    @Resource
    TsDmService tsDmService;

    /**
     * 将协执单位表 存成 <行业类别，协执单位列表> 键值对
     *
     * @return Map<String, List < CkXzdw>>
     */
    public Map<String, List<CkXzdw>> queryXzdw() {

        List<TsDm> tsDmList = tsDmService.queryCkfw();
        Map<String, String> codeMcMap = new HashMap<>();
        for (TsDm tsDm : tsDmList) {
            codeMcMap.put(tsDm.getCode(), tsDm.getMc());
        }

        //查出协执单位表
        List<CkXzdw> xzdwList = xzdwMapper.queryXzdw();

        //存放<行业类别，协执单位列表>
        Map<String, List<CkXzdw>> map = new HashMap<>();
        for (CkXzdw xzdw : xzdwList) {
            String xzdwfl = xzdw.getXzdwfl();

            //翻译行业类别
            String mc = codeMcMap.get(xzdwfl);
            if (!StringUtils.hasText(mc)) {

                //如果翻译不到说明是脏数据获取被禁用了不展示
                continue;
            }

            //如果 mc 存在，返回对应的value，list中新增，如果不存在，插入新的键值
            List<CkXzdw> ckXzdws = map.computeIfAbsent(mc, (k) -> new ArrayList<>());
            ckXzdws.add(xzdw);

        }

        return map;
    }
}
